package com.qdxwx.manage.controller.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.order.OrderDetailService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.OrderStatus;
import com.qdxwx.models.enums.OrderType;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.order.OrderDetail;
import com.qdxwx.models.order.OrderDetailQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course")
public class CourseController extends BaseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private OrderDetailService orderDetailService;

    //课程列表-主
    @Permissions("course:list")
    @GetMapping("/list.html")
    public String course(HttpServletRequest request, ModelMap mmap) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        Channel channel = new Channel();
        channel.setModuleId(2);
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel,null);
        if(channelId < 1 && channelList.size() > 0) {
            channelId = channelList.get(0).getChannelId();
        }
        mmap.put("channelId", channelId);
        mmap.put("channelList", channelList);
        return "course/course-list";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        String search = request.getParameter("search");
        if (TKConvert.toInteger(search) > 0) {
            Course course = courseService.get(TKConvert.toInteger(search));
            if (course == null) return TableDataInfo.error("找不到指定id的课程");
            List<Course> list = new ArrayList<>();
            list.add(course);
            return TableDataInfo.success(list, 1);
        }
        int isAvailable = TKConvert.toInteger(request.getParameter("isAvailable"));
        CourseQuery course = new CourseQuery();
        course.setChannelId(channelId);
        course.setCourseName(TKString.isNotEmpty(search) ? search : null);
        course.setIsAvailable(isAvailable > 0 ? isAvailable : null);
        List<Course> list = courseService.getList(course, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseService.count(course));
        return rspData;
    }

    @GetMapping("/order/list.html")
    public String orderCourse(HttpServletRequest request, ModelMap mmap) {
        int infoId = TKConvert.toInteger(request.getParameter("infoId"));
        if(infoId < 1) return error404;
        Manager manager = SessionUtils.getManager();
        OrderDetailQuery query = new OrderDetailQuery();
        if(manager.getCompanyId() > 1){
            query.setCompanyId(manager.getCompanyId());
        }
        query.setCourseId(infoId);
        query.setAuthStatus(OrderStatus.SUCCESS.toString());
        query.setOrderType(OrderType.COURSE.toString());
        mmap.put("orderCount", orderDetailService.count(query));
        mmap.put("orderSum", orderDetailService.sumPrice(query));
        mmap.put("infoId", infoId);
        return "course/course-order-list";
    }

    /**
     * 查询订单流水列表
     */
    @PostMapping("/order/list.json")
    @ResponseBody
    public TableDataInfo orderCourse(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        int infoId = TKConvert.toInteger(request.getParameter("infoId"));
        if(infoId < 1){
            return null;
        }
        OrderDetailQuery query = new OrderDetailQuery();
        if(manager.getCompanyId() > 1){
            query.setCompanyId(manager.getCompanyId());
        }
        query.setCourseId(infoId);
        query.setOrderNo(TKString.queryKey(request.getParameter("orderNo")));
        query.setCourseName(TKString.queryKey(request.getParameter("courseName")));
        query.setAuthStatus(OrderStatus.SUCCESS.toString());
        query.setOrderType(OrderType.COURSE.toString());
        List<OrderDetail> list = orderDetailService.getList(query, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(orderDetailService.count(query));
        return rspData;
    }
}
